<template>
  <div class="app-container">
    <h2 style="text-align: center;">发布新课程</h2>
    <el-steps
      :active="2"
      process-status="wait"
      align-center
      style="marginbottom: 40px;"
    >
      <el-step title="填写课程基本信息" />
      <el-step title="创建课程大纲" />
      <el-step title="提交审核" />
    </el-steps>

    <!-- 添加章节 -->
    <el-button type="text" @click="dialogChapterFormVisible = true"
      >添加章节</el-button
    >

    <!-- 添加和修改章节表单 -->
    <el-dialog :visible.sync="dialogChapterFormVisible" title="添加章节">
      <el-form :model="dialogChapter" label-width="120px">
        <el-form-item label="章节标题">
          <el-input v-model="dialogChapter.title" />
        </el-form-item>
        <el-form-item label="章节排序">
          <el-input-number
            v-model="dialogChapter.sort"
            :min="0"
            controlsposition="right"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChapterFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加和修改课时表单 -->
    <el-dialog :visible.sync="dialogVideoFormVisible" title="添加小节">
      <el-form :model="dialogVideo" label-width="120px">
        <el-form-item label="课时标题">
          <el-input v-model="dialogVideo.title" />
        </el-form-item>
        <el-form-item label="课时排序">
          <el-input-number
            v-model="dialogVideo.sort"
            :min="0"
            controlsposition="right"
          />
        </el-form-item>
        <el-form-item label="是否免费">
          <el-radio-group v-model="dialogVideo.isFree">
            <el-radio :label="true">免费</el-radio>
            <el-radio :label="false">默认</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="上传视频">
          <!-- TODO -->
          <el-upload
            :on-success="handleVodUploadSuccess"
            :on-remove="handleVodRemove"
            :before-remove="beforeVodRemove"
            :on-exceed="handleUploadExceed"
            :file-list="fileList"
            :action="BASE_API + '/eduVod/video/uploadAlyVideo'"
            :limit="1"
            class="upload-demo"
          >
            <el-button size="small" type="primary">上传视频</el-button>
            <el-tooltip placement="right-end">
              <div slot="content">
                最大支持1G，<br />
                支持3GP、ASF、AVI、DAT、DV、FLV、F4V、<br />
                GIF、M2T、M4V、MJ2、MJPEG、MKV、MOV、MP4、<br />
                MPE、MPG、MPEG、MTS、OGG、QT、RM、RMVB、<br />
                SWF、TS、VOB、WMV、WEBM 等视频格式上传
              </div>
              <i class="el-icon-question" />
            </el-tooltip>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVideoFormVisible = false">取 消</el-button>
        <el-button
          :disabled="saveVideoBtnDisabled"
          type="primary"
          @click="saveOrUpdateVideo"
          >确 定</el-button
        >
      </div>
    </el-dialog>

    <!-- 章节 -->
    <ul class="chanpterList">
      <li v-for="chapter in chapterVideoList" :key="chapter.id">
        <p>
          {{ chapter.title }}
          <span class="acts">
            <el-button type="text" @click="openEditVideo(chapter.id)"
              >添加小节</el-button
            >
            <el-button style="" type="text" @click="updateChapter(chapter.id)"
              >编辑</el-button
            >
            <el-button type="text" @click="deleteChapter(chapter.id)"
              >删除</el-button
            >
          </span>
        </p>
        <!-- 视频 -->
        <ul class="chanpterList videoList">
          <li v-for="video in chapter.children" :key="video.id">
            <p>
              {{ video.title }}
              <span class="acts">
                <el-button type="text" @click="updateEditVideo(video.id)">
                  编辑
                </el-button>
                <el-button type="text" @click="deleteVideo(video.id)">
                  删除
                </el-button>
              </span>
            </p>
          </li>
        </ul>
      </li>
    </ul>

    <div>
      <el-button @click="previous">上一步</el-button>
      <el-button :disabled="saveBtnDisabled" type="primary" @click="next"
        >下一步</el-button
      >
    </div>

    <!-- <el-form label-width="120px">
      <el-form-item>
        <el-button @click="previous">上一步</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="next">
          下一步
        </el-button>
      </el-form-item>
    </el-form> -->
  </div>
</template>
<script>
import chapter from "@/api/edu/chapter";
import video from "@/api/edu/video";

export default {
  data() {
    return {
      saveBtnDisabled: false, // 保存按钮是否禁用
      saveVideoBtnDisabled: false,
      chapterVideoList: [],
      dialogChapterFormVisible: false, //章节弹框值
      dialogVideoFormVisible: false,
      dialogChapter: {
        title: "",
        sort: 0,
        courseId: ""
      },
      dialogVideo: {
        // 课时对象
        title: "",
        sort: 0,
        isFree: false,
        videoSourceId: "",
        videoOriginalName: ""
      },
      courseId: "",
      fileList: [], //上传文件列表
      BASE_API: process.env.BASE_API // 接口API地址
    };
  },
  created() {
    console.log("chapter created");
    if (this.$route.params && this.$route.params.id) {
      this.courseId = this.$route.params.id;
      this.dialogChapter.courseId = this.courseId;
    }
    this.getAllChapterVideo();
  },
  methods: {
    // ======================小结操作======================
    //成功回调
    handleVodUploadSuccess(response, file, fileList) {
      this.dialogVideo.videoSourceId = response.data.videoId;
      this.dialogVideo.videoOriginalName = file.name;
    },
    //视图上传多于一个视频
    handleUploadExceed(files, fileList) {
      this.$message.warning("想要重新上传视频，请先删除已上传的视频");
    },
    // 删除视频前
    beforeVodRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
    handleVodRemove(file, fileList) {
      console.log(file)
      video.deleteVideoFromAly(this.dialogVideo.videoSourceId).then(response => {
        this.dialogVideo.videoSourceId = ''
        this.dialogVideo.videoOriginalName = ''
        this.fileList = []
        this.$message({
          type: 'success',
          message: "移除成功"
        })
      })
    },
    // 删除小结
    deleteVideo(videoId) {
      // 弹窗
      this.$confirm("此操作将永久删除该小节, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        video.deleteVideo(videoId).then(res => {
          if (res.code === 20000) {
            // 提示信息
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            // 刷新页面
            this.getAllChapterVideo();
          }
        });
      });
    },
    // 修改小节
    updateEditVideo(videoId) {
      this.dialogVideoFormVisible = true;
      video.getVideoById(videoId).then(response => {
        this.dialogVideo = response.data.video
      });
    },

    // 添加小结
    openEditVideo(chapterId) {
      this.dialogVideoFormVisible = true;
      this.dialogVideo.chapterId = chapterId;
      this.dialogVideo.courseId = this.courseId;
      // 清空添加小节表单
      this.clearDialogVideo();
    },
    saveOrUpdateVideo() {
      if (this.dialogVideo.id) {
        this.updateVideo();
      } else {
        this.addVideo();
      }

      this.dialogVideoFormVisible = false;
    },
    clearDialogVideo() {
      this.dialogVideo.title = "";
      this.dialogVideo.sort = 1;
      this.dialogVideo.isFree = false;
      this.videoSourceId = "";
      this.fileList = []
    },
    addVideo() {
      video.addVideo(this.dialogVideo).then(response => {
        if (response.code === 20000) {
          // 提示信息
          this.$message({
            type: "success",
            message: "添加成功!"
          });
          // 刷新页面
          this.getAllChapterVideo();
        }
      });
    },
    updateVideo() {
      video.updateVideo(this.dialogVideo).then(response => {
        if (response.code === 20000) {
          // 提示信息
          this.$message({
            type: "success",
            message: "修改成功!"
          });
          // 刷新页面
          this.getAllChapterVideo();
        }
      });
    },

    // ======================章节操作======================
    // 修改章节
    updateChapter(chapterId) {
      chapter.getChapter(chapterId).then(response => {
        this.dialogChapter = response.data.chapter;
      });
      this.dialogChapterFormVisible = true;
    },
    // 删除章节
    deleteChapter(chapterId) {
      // 弹窗
      this.$confirm("此操作将永久删除该章节, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        chapter.deleteChapter(chapterId).then(response => {
          if (response.code === 20000) {
            if (response.data.code && response.data.code === 20001) {
              this.$message({
                type: "success",
                message: "不能删除!"
              });
            } else {
              // 提示信息
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              // 刷新页面
              this.getAllChapterVideo();
            }
          }
        });
      });
    },
    // 添加章节
    saveOrUpdate() {
      if (this.dialogChapter.id) {
        // 修改章节
        chapter.updateChapter(this.dialogChapter).then(response => {
          // 关闭弹窗
          this.dialogChapterFormVisible = false;
          // 提示信息
          this.$message({
            type: "success",
            message: "保存成功!"
          });
          // 刷新页面
          this.getAllChapterVideo();
          // 清空弹窗
          this.dialogChapter.title = "";
          this.dialogChapter.sort = 0;
        });
      } else {
        chapter.addChapter(this.dialogChapter).then(response => {
          // 关闭弹窗
          this.dialogChapterFormVisible = false;
          // 提示信息
          this.$message({
            type: "success",
            message: "保存成功!"
          });
          // 刷新页面
          this.getAllChapterVideo();
          // 清空弹窗
          this.dialogChapter.title = "";
          this.dialogChapter.sort = 0;
        });
      }
    },
    getAllChapterVideo() {
      chapter.getAllChapterVideo(this.courseId).then(response => {
        this.chapterVideoList = response.data.allChapterVideos;
      });
    },

    previous() {
      console.log("previous");
      this.$router.push({ path: "/course/info/" + this.courseId });
    },
    next() {
      console.log("next");
      this.$router.push({ path: "/course/publish/" + this.courseId });
    }
  }
};
</script>

<style scoped>
.chanpterList {
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.chanpterList li {
  position: relative;
}
.chanpterList p {
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #ddd;
}
.chanpterList .acts {
  float: right;
  font-size: 14px;
}
.videoList {
  padding-left: 50px;
}
.videoList p {
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #ddd;
}
</style>
